home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bison1_1.zip / bison-1.11 / conflicts.c < prev    next >
C/C++ Source or Header  |  1992-06-11  |  15KB  |  765 lines

  1. /* Find and resolve or report look-ahead conflicts for bison,
  2.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "system.h"
  23. #include "machine.h"
  24. #include "new.h"
  25. #include "files.h"
  26. #include "gram.h"
  27. #include "state.h"
  28.  
  29.  
  30. #ifdef USG
  31. #include <memory.h>
  32. #define bcopy(src, dst, num) memcpy((dst), (src), (num))
  33. #endif
  34.  
  35. #ifdef __GNUC__
  36. #define alloca __builtin_alloca
  37. #else
  38. #ifdef sparc
  39. #include <alloca.h>
  40. #else
  41. extern char *alloca ();
  42. #endif
  43. #endif
  44.  
  45. extern char **tags;
  46. extern int tokensetsize;
  47. extern char *consistent;
  48. extern short *accessing_symbol;
  49. extern shifts **shift_table;
  50. extern unsigned *LA;
  51. extern short *LAruleno;
  52. extern short *lookaheads;
  53. extern int verboseflag;
  54.  
  55. void set_conflicts();
  56. void resolve_sr_conflict();
  57. void flush_shift();
  58. void log_resolution();
  59. void total_conflicts();
  60. void count_sr_conflicts();
  61. void count_rr_conflicts();
  62.  
  63. char any_conflicts;
  64. char *conflicts;
  65. errs **err_table;
  66. int expected_conflicts;
  67.  
  68.  
  69. static unsigned *shiftset;
  70. static unsigned *lookaheadset;
  71. static int src_total;
  72. static int rrc_total;
  73. static int src_count;
  74. static int rrc_count;
  75.  
  76.  
  77. void
  78. initialize_conflicts()
  79. {
  80.   register int i;
  81. /*  register errs *sp; JF unused */
  82.  
  83.   conflicts = NEW2(nstates, char);
  84.   shiftset = NEW2(tokensetsize, unsigned);
  85.   lookaheadset = NEW2(tokensetsize, unsigned);
  86.  
  87.   err_table = NEW2(nstates, errs *);
  88.  
  89.   any_conflicts = 0;
  90.  
  91.   for (i = 0; i < nstates; i++)
  92.     set_conflicts(i);
  93. }
  94.  
  95.  
  96. void
  97. set_conflicts(state)
  98. int state;
  99. {
  100.   register int i;
  101.   register int k;
  102.   register shifts *shiftp;
  103.   register unsigned *fp2;
  104.   register unsigned *fp3;
  105.   register unsigned *fp4;
  106.   register unsigned *fp1;
  107.   register int symbol;
  108.  
  109.   if (consistent[state]) return;
  110.  
  111.   for (i = 0; i < tokensetsize; i++)
  112.     lookaheadset[i] = 0;
  113.  
  114.   shiftp = shift_table[state];
  115.   if (shiftp)
  116.     {
  117.       k = shiftp->nshifts;
  118.       for (i = 0; i < k; i++)
  119.     {
  120.       symbol = accessing_symbol[shiftp->shifts[i]];
  121.       if (ISVAR(symbol)) break;
  122.       SETBIT(lookaheadset, symbol);
  123.     }
  124.     }
  125.  
  126.   k = lookaheads[state + 1];
  127.   fp4 = lookaheadset + tokensetsize;
  128.  
  129.   /* loop over all rules which require lookahead in this state */
  130.   /* first check for shift-reduce conflict, and try to resolve using precedence  */
  131.  
  132.   for (i = lookaheads[state]; i < k; i++)
  133.     if (rprec[LAruleno[i]])
  134.       {
  135.     fp1 = LA + i * tokensetsize;
  136.     fp2 = fp1;
  137.     fp3 = lookaheadset;
  138.   
  139.     while (fp3 < fp4)
  140.       {
  141.         if (*fp2++ & *fp3++)
  142.           {
  143.         resolve_sr_conflict(state, i);
  144.         break;
  145.           }
  146.       }
  147.       }
  148.  
  149.   /* loop over all rules which require lookahead in this state */
  150.   /* Check for conflicts not resolved above.  */
  151.  
  152.   for (i = lookaheads[state]; i < k; i++)
  153.     {
  154.       fp1 = LA + i * tokensetsize;
  155.       fp2 = fp1;
  156.       fp3 = lookaheadset;
  157.  
  158.       while (fp3 < fp4)
  159.     {
  160.       if (*fp2++ & *fp3++)
  161.         {
  162.           conflicts[state] = 1;
  163.           any_conflicts = 1;
  164.         }
  165.     }
  166.  
  167.       fp2 = fp1;
  168.       fp3 = lookaheadset;
  169.  
  170.       while (fp3 < fp4)
  171.     *fp3++ |= *fp2++;
  172.     }
  173. }
  174.  
  175.  
  176.  
  177. /* Attempt to resolve shift-reduce conflict for one rule
  178. by means of precedence declarations.
  179. It has already been checked that the rule has a precedence.
  180. A conflict is resolved by modifying the shift or reduce tables
  181. so that there is no longer a conflict.  */
  182.  
  183. void
  184. resolve_sr_conflict(state, lookaheadnum)
  185. int state;
  186. int lookaheadnum;
  187. {
  188.   register int i;
  189.   register int mask;
  190.   register unsigned *fp1;
  191.   register unsigned *fp2;
  192.   register int redprec;
  193.   errs *errp = (errs *) alloca (sizeof(errs) + ntokens * sizeof(short));
  194.   short *errtokens = errp->errs;
  195.  
  196.   /* find the rule to reduce by to get precedence of reduction  */
  197.   redprec = rprec[LAruleno[lookaheadnum]];
  198.  
  199.   mask = 1;
  200.   fp1 = LA + lookaheadnum * tokensetsize;
  201.   fp2 = lookaheadset;
  202.   for (i = 0; i < ntokens; i++)
  203.     {
  204.       if ((mask & *fp2 & *fp1) && sprec[i])
  205.     /* shift-reduce conflict occurs for token number i and it has a precision.
  206.        The precedence of shifting is that of token i.  */
  207.     {
  208.       if (sprec[i] < redprec)
  209.         {
  210.           if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  211.           *fp2 &= ~mask;  /* flush the shift for this token */
  212.           flush_shift(state, i);
  213.         }
  214.       else if (sprec[i] > redprec)
  215.         {
  216.           if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  217.           *fp1 &= ~mask;  /* flush the reduce for this token */
  218.         }
  219.       else
  220.         {
  221.           /* Matching precedence levels.
  222.          For left association, keep only the reduction.
  223.          For right association, keep only the shift.
  224.          For nonassociation, keep neither.  */
  225.  
  226.           switch (sassoc[i])
  227.         {
  228.  
  229.         case RIGHT_ASSOC:
  230.               if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  231.           break;
  232.  
  233.         case LEFT_ASSOC:
  234.               if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  235.           break;
  236.  
  237.         case NON_ASSOC:
  238.               if (verboseflag) log_resolution(state, lookaheadnum, i, "an error");
  239.           break;
  240.         }
  241.  
  242.           if (sassoc[i] != RIGHT_ASSOC)
  243.         {
  244.           *fp2 &= ~mask;  /* flush the shift for this token */
  245.           flush_shift(state, i);
  246.         }
  247.           if (sassoc[i] != LEFT_ASSOC)
  248.             {
  249.           *fp1 &= ~mask;  /* flush the reduce for this token */
  250.         }
  251.           if (sassoc[i] == NON_ASSOC)
  252.         {
  253.           /* Record an explicit error for this token.  */
  254.           *errtokens++ = i;
  255.         }
  256.         }
  257.     }
  258.  
  259.       mask <<= 1;
  260.       if (mask == 0)
  261.     {
  262.       mask = 1;
  263.       fp2++;  fp1++;
  264.     }
  265.     }
  266.   errp->nerrs = errtokens - errp->errs;
  267.   if (errp->nerrs)
  268.     {
  269.       /* Some tokens have been explicitly made errors.  Allocate
  270.      a permanent errs structure for this state, to record them.  */
  271.       i = (char *) errtokens - (char *) errp;
  272.       err_table[state] = (errs *) mallocate ((unsigned int)i);
  273.       bcopy (errp, err_table[state], i);
  274.     }
  275.   else
  276.     err_table[state] = 0;
  277. }
  278.  
  279.  
  280.  
  281. /* turn off the shift recorded for the specified token in the specified state.
  282. Used when we resolve a shift-reduce conflict in favor of the reduction.  */
  283.  
  284. void
  285. flush_shift(state, token)
  286. int state;
  287. int token;
  288. {
  289.   register shifts *shiftp;
  290.   register int k, i;
  291. /*  register unsigned symbol; JF unused */
  292.  
  293.   shiftp = shift_table[state];
  294.  
  295.   if (shiftp)
  296.     {
  297.       k = shiftp->nshifts;
  298.       for (i = 0; i < k; i++)
  299.     {
  300.       if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]])
  301.         (shiftp->shifts[i]) = 0;
  302.     }
  303.     }
  304. }
  305.  
  306.  
  307. void
  308. log_resolution(state, LAno, token, resolution)
  309. int state, LAno, token;
  310. char *resolution;
  311. {
  312.   fprintf(foutput,
  313.       "Conflict in state %d between rule %d and token %s resolved as %s.\n",
  314.       state, LAruleno[LAno], tags[token], resolution);
  315. }
  316.  
  317.  
  318. void
  319. conflict_log()
  320. {
  321.   register int i;
  322.  
  323.   src_total = 0;
  324.   rrc_total = 0;
  325.  
  326.   for (i = 0; i < nstates; i++)
  327.     {
  328.       if (conflicts[i])
  329.     {
  330.       count_sr_conflicts(i);
  331.       count_rr_conflicts(i);
  332.       src_total += src_count;
  333.       rrc_total += rrc_count;
  334.     }
  335.     }
  336.  
  337.   total_conflicts();
  338. }
  339.   
  340.  
  341. void
  342. verbose_conflict_log()
  343. {
  344.   register int i;
  345.  
  346.   src_total = 0;
  347.   rrc_total = 0;
  348.  
  349.   for (i = 0; i < nstates; i++)
  350.     {
  351.       if (conflicts[i])
  352.     {
  353.       count_sr_conflicts(i);
  354.       count_rr_conflicts(i);
  355.       src_total += src_count;
  356.       rrc_total += rrc_count;
  357.  
  358.       fprintf(foutput, "State %d contains", i);
  359.  
  360.       if (src_count == 1)
  361.         fprintf(foutput, " 1 shift/reduce conflict");
  362.       else if (src_count > 1)
  363.         fprintf(foutput, " %d shift/reduce conflicts", src_count);
  364.  
  365.       if (src_count > 0 && rrc_count > 0)
  366.         fprintf(foutput, " and");
  367.  
  368.       if (rrc_count == 1)
  369.         fprintf(foutput, " 1 reduce/reduce conflict");
  370.       else if (rrc_count > 1)
  371.         fprintf(foutput, " %d reduce/reduce conflicts", rrc_count);
  372.  
  373.       putc('.', foutput);
  374.       putc('\n', foutput);
  375.     }
  376.     }
  377.  
  378.   total_conflicts();
  379. }
  380.  
  381.  
  382. void
  383. total_conflicts()
  384. {
  385.   extern int fixed_outfiles;
  386.  
  387.   if (src_total == expected_conflicts && rrc_total == 0)
  388.     return;
  389.  
  390.   if (fixed_outfiles)
  391.     {
  392.       /* If invoked under the name `yacc', use the output format
  393.      specified by POSIX.  */
  394.       fprintf(stderr, "conflicts: ", infile);
  395.       if (src_total > 0)
  396.     fprintf(stderr, " %d shift/reduce", src_total);
  397.       if (src_total > 0 && rrc_total > 0)
  398.     fprintf(stderr, ",");
  399.       if (rrc_total > 0)
  400.     fprintf(stderr, " %d reduce/reduce", rrc_total);
  401.       putc('\n', stderr);
  402.     }
  403.   else
  404.     {
  405.       fprintf(stderr, "%s contains", infile);
  406.  
  407.       if (src_total == 1)
  408.     fprintf(stderr, " 1 shift/reduce conflict");
  409.       else if (src_total > 1)
  410.     fprintf(stderr, " %d shift/reduce conflicts", src_total);
  411.  
  412.       if (src_total > 0 && rrc_total > 0)
  413.     fprintf(stderr, " and");
  414.  
  415.       if (rrc_total == 1)
  416.     fprintf(stderr, " 1 reduce/reduce conflict");
  417.       else if (rrc_total > 1)
  418.     fprintf(stderr, " %d reduce/reduce conflicts", rrc_total);
  419.  
  420.       putc('.', stderr);
  421.       putc('\n', stderr);
  422.     }
  423. }
  424.  
  425.  
  426. void
  427. count_sr_conflicts(state)
  428. int state;
  429. {
  430.   register int i;
  431.   register int k;
  432.   register int mask;
  433.   register shifts *shiftp;
  434.   register unsigned *fp1;
  435.   register unsigned *fp2;
  436.   register unsigned *fp3;
  437.   register int symbol;
  438.  
  439.   src_count = 0;
  440.  
  441.   shiftp = shift_table[state];
  442.   if (!shiftp) return;
  443.  
  444.   for (i = 0; i < tokensetsize; i++)
  445.     {
  446.       shiftset[i] = 0;
  447.       lookaheadset[i] = 0;
  448.     }
  449.  
  450.   k = shiftp->nshifts;
  451.   for (i = 0; i < k; i++)
  452.     {
  453.       if (! shiftp->shifts[i]) continue;
  454.       symbol = accessing_symbol[shiftp->shifts[i]];
  455.       if (ISVAR(symbol)) break;
  456.       SETBIT(shiftset, symbol);
  457.     }
  458.  
  459.   k = lookaheads[state + 1];
  460.   fp3 = lookaheadset + tokensetsize;
  461.  
  462.   for (i = lookaheads[state]; i < k; i++)
  463.     {
  464.       fp1 = LA + i * tokensetsize;
  465.       fp2 = lookaheadset;
  466.  
  467.       while (fp2 < fp3)
  468.     *fp2++ |= *fp1++;
  469.     }
  470.  
  471.   fp1 = shiftset;
  472.   fp2 = lookaheadset;
  473.  
  474.   while (fp2 < fp3)
  475.     *fp2++ &= *fp1++;
  476.  
  477.   mask = 1;
  478.   fp2 = lookaheadset;
  479.   for (i = 0; i < ntokens; i++)
  480.     {
  481.       if (mask & *fp2)
  482.     src_count++;
  483.  
  484.       mask <<= 1;
  485.       if (mask == 0)
  486.     {
  487.       mask = 1;
  488.       fp2++;
  489.     }
  490.     }
  491. }
  492.  
  493.  
  494. void
  495. count_rr_conflicts(state)
  496. int state;
  497. {
  498.   register int i;
  499.   register int j;
  500.   register int count;
  501.   register unsigned mask;
  502.   register unsigned *baseword;
  503.   register unsigned *wordp;
  504.   register int m;
  505.   register int n;
  506.  
  507.   rrc_count = 0;
  508.  
  509.   m = lookaheads[state];
  510.   n = lookaheads[state + 1];
  511.  
  512.   if (n - m < 2) return;
  513.  
  514.   mask = 1;
  515.   baseword = LA + m * tokensetsize;
  516.   for (i = 0; i < ntokens; i++)
  517.     {
  518.       wordp = baseword;
  519.  
  520.       count = 0;
  521.       for (j = m; j < n; j++)
  522.     {
  523.       if (mask & *wordp)
  524.         count++;
  525.  
  526.       wordp += tokensetsize;
  527.     }
  528.  
  529.       if (count >= 2) rrc_count++;
  530.  
  531.       mask <<= 1;
  532.       if (mask == 0)
  533.     {
  534.       mask = 1;
  535.       baseword++;
  536.     }
  537.     }
  538. }
  539.  
  540.  
  541. void
  542. print_reductions(state)
  543. int state;
  544. {
  545.   register int i;
  546.   register int j;
  547.   register int k;
  548.   register unsigned *fp1;
  549.   register unsigned *fp2;
  550.   register unsigned *fp3;
  551.   register unsigned *fp4;
  552.   register int rule;
  553.   register int symbol;
  554.   register unsigned mask;
  555.   register int m;
  556.   register int n;
  557.   register int default_LA;
  558.   register int default_rule;
  559.   register int cmax;
  560.   register int count;
  561.   register shifts *shiftp;
  562.   register errs *errp;
  563.   int nodefault = 0;
  564.  
  565.   for (i = 0; i < tokensetsize; i++)
  566.     shiftset[i] = 0;
  567.  
  568.   shiftp = shift_table[state];
  569.   if (shiftp)
  570.     {
  571.       k = shiftp->nshifts;
  572.       for (i = 0; i < k; i++)
  573.     {
  574.       if (! shiftp->shifts[i]) continue;
  575.       symbol = accessing_symbol[shiftp->shifts[i]];
  576.       if (ISVAR(symbol)) break;
  577.       /* if this state has a shift for the error token,
  578.          don't use a default rule.  */
  579.       if (symbol == error_token_number) nodefault = 1;
  580.       SETBIT(shiftset, symbol);
  581.     }
  582.     }
  583.  
  584.   errp = err_table[state];
  585.   if (errp)
  586.     {
  587.       k = errp->nerrs;
  588.       for (i = 0; i < k; i++)
  589.     {
  590.       if (! errp->errs[i]) continue;
  591.       symbol = errp->errs[i];
  592.       SETBIT(shiftset, symbol);
  593.     }
  594.     }
  595.  
  596.   m = lookaheads[state];
  597.   n = lookaheads[state + 1];
  598.  
  599.   if (n - m == 1 && ! nodefault)
  600.     {
  601.       default_rule = LAruleno[m];
  602.  
  603.       fp1 = LA + m * tokensetsize;
  604.       fp2 = shiftset;
  605.       fp3 = lookaheadset;
  606.       fp4 = lookaheadset + tokensetsize;
  607.  
  608.       while (fp3 < fp4)
  609.     *fp3++ = *fp1++ & *fp2++;
  610.  
  611.       mask = 1;
  612.       fp3 = lookaheadset;
  613.  
  614.       for (i = 0; i < ntokens; i++)
  615.     {
  616.       if (mask & *fp3)
  617.         fprintf(foutput, "    %-4s\t[reduce using rule %d (%s)]\n",
  618.             tags[i], default_rule, tags[rlhs[default_rule]]);
  619.  
  620.       mask <<= 1;
  621.       if (mask == 0)
  622.         {
  623.           mask = 1;
  624.           fp3++;
  625.         }
  626.     }
  627.  
  628.       fprintf(foutput, "    $default\treduce using rule %d (%s)\n\n",
  629.           default_rule, tags[rlhs[default_rule]]);
  630.     }
  631.   else if (n - m >= 1)
  632.     {
  633.       cmax = 0;
  634.       default_LA = -1;
  635.       fp4 = lookaheadset + tokensetsize;
  636.  
  637.       if (! nodefault)
  638.     for (i = m; i < n; i++)
  639.       {
  640.         fp1 = LA + i * tokensetsize;
  641.         fp2 = shiftset;
  642.         fp3 = lookaheadset;
  643.   
  644.         while (fp3 < fp4)
  645.           *fp3++ = *fp1++ & ( ~ (*fp2++));
  646.   
  647.         count = 0;
  648.         mask = 1;
  649.         fp3 = lookaheadset;
  650.         for (j = 0; j < ntokens; j++)
  651.           {
  652.         if (mask & *fp3)
  653.           count++;
  654.   
  655.         mask <<= 1;
  656.         if (mask == 0)
  657.           {
  658.             mask = 1;
  659.             fp3++;
  660.           }
  661.           }
  662.   
  663.         if (count > cmax)
  664.           {
  665.         cmax = count;
  666.         default_LA = i;
  667.         default_rule = LAruleno[i];
  668.           }
  669.   
  670.         fp2 = shiftset;
  671.         fp3 = lookaheadset;
  672.   
  673.         while (fp3 < fp4)
  674.           *fp2++ |= *fp3++;
  675.       }
  676.  
  677.       for (i = 0; i < tokensetsize; i++)
  678.         shiftset[i] = 0;
  679.  
  680.       if (shiftp)
  681.         {
  682.           k = shiftp->nshifts;
  683.           for (i = 0; i < k; i++)
  684.         {
  685.           if (! shiftp->shifts[i]) continue;
  686.           symbol = accessing_symbol[shiftp->shifts[i]];
  687.           if (ISVAR(symbol)) break;
  688.           SETBIT(shiftset, symbol);
  689.         }
  690.         }
  691.  
  692.       mask = 1;
  693.       fp1 = LA + m * tokensetsize;
  694.       fp2 = shiftset;
  695.       for (i = 0; i < ntokens; i++)
  696.     {
  697.       int defaulted = 0;
  698.  
  699.       if (mask & *fp2)
  700.         count = 1;
  701.       else
  702.         count = 0;
  703.  
  704.       fp3 = fp1;
  705.       for (j = m; j < n; j++)
  706.         {
  707.           if (mask & *fp3)
  708.         {
  709.           if (count == 0)
  710.             {
  711.               if (j != default_LA)
  712.             {
  713.               rule = LAruleno[j];
  714.               fprintf(foutput, "    %-4s\treduce using rule %d (%s)\n",
  715.                   tags[i], rule, tags[rlhs[rule]]);
  716.             }
  717.               else defaulted = 1;
  718.  
  719.               count++;
  720.             }
  721.           else
  722.             {
  723.               if (defaulted)
  724.             {
  725.               rule = LAruleno[default_LA];
  726.               fprintf(foutput, "    %-4s\treduce using rule %d (%s)\n",
  727.                   tags[i], rule, tags[rlhs[rule]]);
  728.               defaulted = 0;
  729.             }
  730.               rule = LAruleno[j];
  731.               fprintf(foutput, "    %-4s\t[reduce using rule %d (%s)]\n",
  732.                   tags[i], rule, tags[rlhs[rule]]);
  733.             }
  734.         }
  735.  
  736.           fp3 += tokensetsize;
  737.         }
  738.  
  739.       mask <<= 1;
  740.       if (mask == 0)
  741.         {
  742.           mask = 1;
  743.           fp1++;
  744.         }
  745.     }
  746.  
  747.       if (default_LA >= 0)
  748.     {
  749.       fprintf(foutput, "    $default\treduce using rule %d (%s)\n",
  750.           default_rule, tags[rlhs[default_rule]]);
  751.     }
  752.  
  753.       putc('\n', foutput);
  754.     }
  755. }
  756.  
  757.  
  758. void
  759. finalize_conflicts()
  760. {
  761.   FREE(conflicts);
  762.   FREE(shiftset);
  763.   FREE(lookaheadset);
  764. }
  765.